/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： mysite
 * 文件名称： articleedit.js
 * Date：18-9-26 下午5:54
 * Author: guomw
 */
$(function () {
    $('input[type="file"]').prettyFile();
    var smnote = $(".summernote").summernote({
        lang: "zh-CN",
        onImageUpload: function (files, editor, $editable) {
            hotUtil.uploadImg(files,"article",function (url) {
                editor.insertImage($editable, url);
            });
        }
    });
    articleHandler.getClassifyList();
    articleHandler.init();
});


var articleHandler = {
    classifyId: 0,
    params: {
        articleId: hotUtil.getQuery("articleId")
    },
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/article/ajax/getarticleinfo", self.params, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000 && ret.data != null) {
                $("#articleTitle").val(ret.data.articleTitle);
                $("#articleIntro").val(ret.data.articleIntro);
                $("#txtcover").val(ret.data.articleCover);
                $(".imgpreview").attr("src", ret.data.articleCover);
                self.setEditContent(hotUtil.decode(ret.data.articleBody));
                if (ret.data.classify != null) {
                    self.classifyId = ret.data.classify.classifyId;
                    $("#sl_classify").val(ret.data.classify.classifyId);
                }
            }
        })
    },
    edit: function () {
        var self = this;
        if (hotUtil.isNullOrEmpty($("#articleTitle").val())) {
            swal("请输入文章标题", "", "warning")
            $("#articleTitle").focus();
            return false;
        }
        self.classifyId=$("#sl_classify").val();
        if(parseInt(self.classifyId)<=0){
            swal("请选择文章分类", "", "warning")
            return false;
        }
        if ($.trim(hotUtil.isNullOrEmpty(self.getEditContent())).length == 0) {
            swal("文章内容不能为空", "", "warning")
            return false;
        }
        var param = {
            classifyId: self.classifyId,
            articleTitle: $("#articleTitle").val(),
            articleBody: hotUtil.encode(self.getEditContent()),
            articleId: self.params.articleId,
            articleIntro: $("#articleIntro").val(),
            articleCover: $("#txtcover").val()
        }
        hotUtil.loading.show();
        if (!hotUtil.isNullOrEmpty($("#uploadfile").val())) {
            hotUtil.uploadImg($("#uploadfile")[0].files,"article", function (url) {
                param.articleCover = url;
                $(".imgpreview").attr("src", url);
                self.submit(param);
            });
        }
        else {
            self.submit(param);
        }
    },
    submit: function (param) {
        var self = this;
        hotUtil.ajaxCall('/article/ajax/editarticle', param, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                if (self.params.articleId > 0)
                    self.init();
                swal("提交成功", "", "success");
            }
            else {
                swal("提交失败", "", "error");
            }
        })
    },

    getEditContent: function () {
        return $(".summernote").code();
    },
    setEditContent: function (content) {
        $(".summernote").code(content)
    },
    getClassifyList: function () {
        var self = this;
        hotUtil.ajaxCallGet("/article/ajax/classifylist", {}, function (ret) {
            if (ret.resultCode === 2000 && ret.data != null) {
                var tempHtml = '<option value="-1">请选择分类</option>';
                $.each(ret.data, function (i, item) {
                    tempHtml += '<option value="' + item.classifyId + '">' + item.classifyTitle + '</option>';
                });
                $("#sl_classify").html(tempHtml);
                if (self.classifyId > 0) {
                    $("#sl_classify").val(self.classifyId);
                }
            }
        });
    }
}

